'  
' SolutionCleaner.vb @Microsoft Visual Studio 2008 <.NET Framework 2.0> 
' AfritXia 
' 2009/11/21
' 
' $Rev: 13 $ 
' 
' 清理解决方案
' 

Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.IO

Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90

''' <summary>
''' 清理解决方案
''' </summary>
Public Class SolutionCleaner
	''' <summary>
	''' 解决方案目录
	''' </summary>
	Private _solutionDir As String = Nothing

	''' <summary>
	''' 结果文本
	''' </summary>
	Private _resultText As String = Nothing

	''' <summary>
	''' 类参数构造器
	''' </summary>
	''' <param name ="solutionDir"></param>
	Public Sub New(ByVal solutionDir As String)
		If (String.IsNullOrEmpty(solutionDir)) Then
			Throw New ArgumentException("参数为空", "solutionDir")
		End If

		Me._solutionDir = solutionDir
	End Sub

	''' <summary>
	''' 获取解决方案目录
	''' </summary>
	''' <returns></returns>
	Public ReadOnly Property SolutionDir() As String
		Get
			Return Me._solutionDir
		End Get
	End Property

	''' <summary>
	''' 获取结果文本
	''' </summary>
	''' <returns></returns>
	Public ReadOnly Property ResultText() As String
		Get
			Return Me._resultText
		End Get
	End Property

	''' <summary>
	''' 清理解决方案
	''' </summary>
	Public Sub Clean()
		' 获取目录列表
		Dim dirList As IList(Of String) = Me.GetDirList(Me._solutionDir)

		If (dirList Is Nothing) Then
			Exit Sub
		End If

		Me._resultText = "清理解决方案" & vbCrLf & "-" & vbCrLf

		For Each dir As String In dirList
			' 删除 bin 目录
			Me.DelBin(dir & "/bin", Me._resultText)

			' 删除 obj 目录
			Me.DelObj(dir & "/obj", Me._resultText)
		Next

		Me._resultText = Me._resultText & "清理完成"
	End Sub

	''' <summary>
	''' 获取目录列表
	''' </summary>
	''' <returns></returns>
	Private Function GetDirList(ByVal rootDir As String) As IList(Of String)
		If (String.IsNullOrEmpty(rootDir)) Then
			Return Nothing
		End If

		If Not (Directory.Exists(rootDir)) Then
			Return Nothing
		End If

		' 临时队列
		Dim _tempQ As Queue(Of String) = New Queue(Of String)
		' 结果列表
		Dim _resultList As List(Of String) = New List(Of String)

		_tempQ.Enqueue(rootDir)

		While (_tempQ.Count >= 1)
			' 获取当前目录
			Dim currDir As String = _tempQ.Dequeue()

			If (String.IsNullOrEmpty(currDir)) Then
				Continue While
			End If

			_resultList.Add(currDir)

			' 获取子目录
			Dim subDirs As String() = Directory.GetDirectories(currDir)

			If Not (subDirs Is Nothing) Then
				For Each subDir As String In subDirs
					' 子目录加入队列
					_tempQ.Enqueue(subDir)
				Next
			End If
		End While

		Return _resultList
	End Function

	''' <summary>
	''' 删除 bin 目录
	''' </summary>
	''' <param name="bin"></param>
	''' <param name="resultText"></param>
	Private Sub DelBin(ByVal bin As String, ByRef resultText As String)
		' bin
		Dim binDir As DirectoryInfo = New DirectoryInfo(bin)

		' 删除 bin 目录
		If (binDir.Exists) Then
			Try
				' 删除 bin 目录
				binDir.Delete(True)

				' 记录结果
				resultText = resultText & "清理: " & bin & vbCrLf
			Catch ex As UnauthorizedAccessException
				' 忽略 .exe 文件
			Catch ex As Exception
				Throw ex
			End Try
		End If
	End Sub

	''' <summary>
	''' 删除 obj 目录
	''' </summary>
	''' <param name="obj"></param>
	''' <param name="resultText"></param>
	Private Sub DelObj(ByVal obj As String, ByRef resultText As String)
		' obj
		Dim objDir As DirectoryInfo = New DirectoryInfo(obj)

		' 删除 obj 目录
		If (objDir.Exists) Then
			Try
				' 删除 obj 目录
				objDir.Delete(True)

				' 记录结果
				resultText = resultText & "清理: " & obj & vbCrLf
			Catch ex As UnauthorizedAccessException
				' 忽略 .exe 文件
			Catch ex As Exception
				Throw ex
			End Try
		End If
	End Sub

End Class
